<template>
    <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1200px" @open="open" @close="close">
        <el-form class="tl-form" ref="dealform" :model="model" :rules="rules" label-width="120px">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="快递单号" prop="couriernumber" :rules="rules.validateRequire">
                        <el-input v-model="model.couriernumber"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="快递方式" prop="courier" :rules="rules.validateRequire">
                        <el-select v-model="model.courier" filterable clearable>
                            <el-option v-for="item in codeModel.courierList" :key="item.code" :label="item.name" :value="item.code" />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="120px" :disabled="disableEditSubmit || disableSave">
            <el-row>
                <el-col :span="24">
                    <el-divider content-position="center"><span>【以下信息为只读信息】</span></el-divider>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-divider content-position="left"><span>1、【合同】相关信息</span></el-divider>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="项目" prop="projectid" :required="true" :rules="rules.validateRequire">
                        <el-input v-model="model.dispprojectid" :disabled="true" class="tl-ref-selector">
                            <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmProjectRefDialog" v-if="!disableEditSubmit && !disableSave"></el-button>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="合同所属组织" prop="contractorgname" :required="true" :rules="rules.validateRequire">
                        <tl-ref-org v-model="model.contractorgname" :initDisplayData.sync="model.contractorgname" dataType="code" :singleSelect="true" @confirm="onSelectOrg" :disabled="disableEditSubmit || disableSave"></tl-ref-org>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <!-- <el-col :span="12">
                    <el-form-item label="合同所属组织编码" prop="contractorgcode">
                        <el-input v-model="model.contractorgcode"></el-input>
                    </el-form-item>
                </el-col> -->
                <el-col :span="12">
                    <el-form-item label="合同所属部门" prop="contractdeptname" :required="true" :rules="rules.validateRequire">
                        <tl-ref-dept v-model="model.contractdeptname" :initDisplayData.sync="model.contractdeptname" dataType="code" nameType="fullname" :singleSelect="true" @confirm="onSelectDept" :disabled="disableEditSubmit || disableSave" :externalQueryParam="{qry_enabled:1}"></tl-ref-dept>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="客户名称" prop="customername" :required="true" :rules="rules.validateRequire">
                        <el-input v-model="model.customername" :disabled="true" class="tl-ref-selector">
                            <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmCustomerRefDialog" v-if="!disableEditSubmit && !disableSave"></el-button>
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <!-- <el-row>
                 <el-col :span="12">
                    <el-form-item label="合同所属部门编码" prop="contractdeptcode">
                        <el-input v-model="model.contractdeptcode"></el-input>
                    </el-form-item>
                </el-col>
                 <el-col :span="12">
                    <el-form-item label="客户" prop="customerid">
                        <el-input v-model="model.customerid"></el-input>
                    </el-form-item>
                </el-col>
            </el-row> -->
            <el-row>
                <el-col :span="12">
                    <el-form-item label="合同名称" prop="contractname" :rules="rules.validateRequire">
                        <el-input v-model="model.contractname"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="合同日期" prop="contractdate" :rules="rules.validateRequire">
                        <el-date-picker v-model="model.contractdate" type="date" placeholder="合同日期" value-format="yyyy-MM-dd"></el-date-picker>
                    </el-form-item>
                </el-col>
                <!-- <el-col :span="12">
                    <el-form-item label="合同编号" prop="contractcode">
                        <el-input v-model="model.contractcode" :readonly="true" class="is-disabled"></el-input>
                    </el-form-item>
                </el-col> -->
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="本次合同金额" prop="contractmoney" :rules="rules.validateRequire">
                        <el-input-number v-model="model.contractmoney" controls-position="right" :step="1" :min="0" :precision="2" style="width:95%"></el-input-number>
                        <span>元</span>
                    </el-form-item>
                </el-col>
                <el-col :span="11" :offset="1">
                    <el-alert title="提示：若本次合同金额为负值，则项目合同总金额会减去相应值。" type="warning" :closable="false">
                    </el-alert>
                </el-col>
                <!-- <el-col :span="12">
                    <el-form-item label="已开票金额" prop="billmoney">
                        <el-input v-model="model.billmoney" :readonly="true" class="is-disabled"></el-input>
                    </el-form-item>
                </el-col> -->
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="收件人" prop="receiver" :rules="rules.validateRequire">
                        <el-input v-model="model.receiver"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="收件人电话" prop="phone" :rules="rules.validateRequire">
                        <el-input v-model="model.phone"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="回寄地址" prop="postal" :rules="rules.validateRequire">
                        <el-input v-model="model.postal"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="备注" prop="description">
                        <el-input v-model="model.description" type="textarea" :rows="2"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-collapse>
                <el-collapse-item title="【单据】相关信息">
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="申请部门" prop="deptname">
                                <tl-ref-dept v-model="model.deptcode" :initDisplayData.sync="model.deptname" :disabled="true" dataType="code" nameType="fullname" :singleSelect="true" :externalQueryParam="{qry_enabled:1}"></tl-ref-dept>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="申请公司" prop="orgname">
                                <el-input v-model="model.orgname" :readonly="true" class="is-disabled"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="申请人" prop="username">
                                <tl-ref-user v-model="model.username" :initDisplayData.sync="model.username" :disabled="true" dataType="code" :singleSelect="true"></tl-ref-user>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="申请人工号" prop="usercode">
                                <el-input v-model="model.usercode" :readonly="true" class="is-disabled"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="单据日期" prop="docdate" :rules="rules.validateRequire">
                                <el-date-picker v-model="model.docdate" type="date" placeholder="单据日期" value-format="yyyy-MM-dd"></el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="提交时间" prop="submitdate">
                                <el-date-picker v-model="model.submitdate" type="datetime" placeholder="提交时间" disabled></el-date-picker>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="审批流程" prop="flowid">
                                <el-input v-model="model.dispflowid" :readonly="true" class="is-disabled"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="状态" prop="state">
                                <el-input v-model="model.dispstate" disabled></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-collapse-item>
            </el-collapse>
        </el-form>
        <tl-approval-history ref="history" sourceType="PmContract" :sourceId="model.id" />
        <div slot="footer" class="dialog-footer">
            <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit" v-if="uiOperateState<=2">取消</el-button>
            <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="!disableEditSubmit" :disabled="disableSave">
                <svg-icon icon-class="save" />保存</el-button>
            <el-button type="primary" @click="doSubmitApproval" :loading="submitLoading" v-if="!disableEditSubmit" :disabled="disableApproval">
                <svg-icon icon-class="send" />提交审批</el-button>
            <tl-attachment sourceType="PmContract" :sourceId="model.id" :disabled="disableAttachment" :editable="stateEdit"></tl-attachment>
            <tl-attachment ref="attachmentSignRef" sourceType="PmSignContract" :sourceId="model.id" btnTitle="合同扫描件" :disabled="disableAttachment" :editable="false"></tl-attachment>
            <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory">
                <svg-icon icon-class="list" />审批历史</el-button>
            <el-button type="primary" @click="doDelApproval" :loading="delApprovalLoading" v-if="!disableEditSubmit" :disabled="disableDelApproval">
                <svg-icon icon-class="undo" />取消审批</el-button>
        </div>
        <tlpm-project-ref ref="pmProjectRef" @confirm="getpmProjectRefData" :isSingleSelect="true"></tlpm-project-ref>
        <tlpm-customer-ref ref="pmCustomerRef" @confirm="getpmCustomerRefData" :isSingleSelect="true"></tlpm-customer-ref>
    </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
/** 子组件 */
import tlpmProjectRef from '@/views/ProjectManage/pmProject/refList.vue'
import tlpmCustomerRef from '@/views/ProjectManage/pmCustomer/refList.vue'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
    mixins: [mixin_approval_edit],
    components: { tlpmProjectRef, tlpmCustomerRef },
    data() {
        return {
            moduleTitle: '项目合同',
            urlPrefix: '/pm/contract/',
            url: {
                // 额外的url
            }
        }
    },
    methods: {
        assignDefaultValue() {
            if (this.isAdd) {
                if (this.codeModel.flowidList && this.codeModel.flowidList.length > 0) {
                    this.$set(this.model, 'flowid', parseInt(this.codeModel.flowidList[0].id))
                }
                this.$set(this.model, 'orgcode', this.org.code)
                this.$set(this.model, 'orgname', this.org.name)
                this.$set(this.model, 'deptcode', this.dept.code)
                this.$set(this.model, 'deptname', this.dept.fullname)
                this.$set(this.model, 'usercode', this.user.code)
                this.$set(this.model, 'username', this.user.name)
            }
        },
        loaded() {
            // 页面加载完毕执行
            if (!this.isAdd && this.model.docno) {
                this.title = this.title + `（单据编号：${this.model.docno}）`
            }
        },
        onSelectDept(selectedDepts) {
            const selectedDept = selectedDepts[0]
            this.$set(this.model, 'contractdeptname', selectedDept.fullname)
            this.$set(this.model, 'contractdeptcode', selectedDept.code)
        },
        onSelectOrg(selectedOrgs) {
            const selectedOrg = selectedOrgs[0]
            this.$set(this.model, 'contractorgname', selectedOrg.name)
            this.$set(this.model, 'contractorgcode', selectedOrg.code)
        },
        /** 打开项目参照 */
        openpmProjectRefDialog() {
            this.$refs.pmProjectRef.visible = true
        },
        /** 获取项目参数数据 */
        getpmProjectRefData(selectionRow, selectKey, selectionValue) {
            this.$set(this.model, 'projectid', selectionRow.id)
            this.$set(this.model, 'dispprojectid', selectionRow.name)
            this.$set(this.model, 'billmoney', selectionRow.billamount)
        },
        /** 打开客户名称参照 */
        openpmCustomerRefDialog() {
            this.$refs.pmCustomerRef.visible = true
        },
        /** 获取客户名称参数数据 */
        getpmCustomerRefData(selectionRow, selectKey, selectionValue) {
            this.$set(this.model, 'customerid', selectionRow.id)
            this.$set(this.model, 'customername', selectionRow.name)
        },
        saveCourierInfo(resolve, reject) {
            const { id, couriernumber, courier } = this.model
            if (!couriernumber) {
                this.$tap.alert('请填写快递单号')
                reject()
            }
            if (!courier) {
                this.$tap.alert('请填写快递方式')
                reject()
            }
            const param = {
                id,
                couriernumber,
                courier
            }
            putAction(this.url.edit, param)
                .then(res => {
                    if (res.success) {
                        resolve()
                    } else {
                        this.$tap.alert(res.message, 'warning')
                        reject()
                    }
                })
                .catch(err => {
                    this.$tap.alert('保存快递信息失败，请联系系统管理员', 'warning')
                    reject()
                })
        }
    }
}
</script>

